//*************************************************************************************
//* 作    者： 
//* 创建时间： 2021-11-23 04:38:31
//*  描    述：

//* ************************************************************************************
using System;
using UnityEngine;
using UFrame.Audio;

public class RotateAudioBehaviour : MonoBehaviour
{
    public float minSpeed = 1f;       //The minimum speed required for audio
    public float maxSpeed = 30f;      //The speed required for maximum audio
    public float minVolume = 0f;      //Minimum volume
    public float maxVolume = 0.6f;     //Maximum volume
    public float minPitch = .4f;      //Minimum pitch
    public float maxPitch = 0.8f;      //Maximum pitch
    public float smoothing = 10;
    public float m_moveRetio = 10;
    public int audioId = 412;
    public int channel = 3;
    private float m_currentRotateSpeed;
    private float m_lastAngle;
    protected AudioContext m_rotateContext;
    protected bool m_rotatePlaying;
    protected bool m_movePlaying;

    private void OnEnable()
    {
        m_rotatePlaying = false;
        m_movePlaying = false;
        m_lastAngle = transform.localEulerAngles.y;
    }

    private void Start()
    {
        m_rotateContext = SoundAgent.Instance.PlayAudioById(audioId, channel, 0, 1);
    }

    private void Update()
    {
        if (m_rotateContext == null || !m_rotateContext.audioSource)
        {
            Debug.LogError("finder audio audio play failed!");
            enabled = false;
            return;
        }
        var distance = Mathf.Abs(transform.localEulerAngles.y - m_lastAngle);
        m_lastAngle = transform.localEulerAngles.y;
        m_currentRotateSpeed = Mathf.Lerp(m_currentRotateSpeed, distance / Time.deltaTime, smoothing * Time.deltaTime);
        RefreshAudio(m_rotateContext.audioSource, m_currentRotateSpeed, ref m_rotatePlaying);
    }

    private void OnDisable()
    {
        if (m_rotatePlaying && m_rotateContext?.audioSource)
        {
            m_rotateContext.audioSource.volume = 0;
            m_rotateContext.audioSource.Pause();
            m_rotatePlaying = false;
        }
    }

    private void OnDestroy()
    {
        if (m_rotateContext)
            m_rotateContext.Stop();
    }

    private void RefreshAudio(AudioSource swordAudio, float speed, ref bool playing)
    {
        //If the volume is below a very small amount, apply it
        if (speed > minSpeed)
        {
            //Calculate the percentage the current speed is between the minimum and maximum
            float percentage = Mathf.Clamp((speed - minSpeed) / (maxSpeed - minSpeed), 0f, 1f);

            //Calculate the desired volume and pitch based on the speed percentage
            float targetPitch = Mathf.Lerp(minPitch, maxPitch, percentage);
            float targetVolume = Mathf.Lerp(minVolume, maxVolume, percentage);

            swordAudio.pitch = targetPitch;
            swordAudio.volume = targetVolume;
            if (!playing)
            {
                swordAudio.UnPause();
                playing = true;
            }
        }
        //...otherwise set the values to their minimum and set the audio to inactive
        else if (playing)
        {
            swordAudio.pitch = minPitch;
            swordAudio.volume = minVolume;
            playing = false;
            swordAudio.Pause();
            swordAudio.time = 0;
        }
    }
}
